package com.example.springsecurityauth2clientalipaylogin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.web.HttpSessionOAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import static org.springframework.security.oauth2.core.AuthorizationGrantType.AUTHORIZATION_CODE;

/**
 * @auth 罗俊华
 * @Date 2022/9/4 9:22 下午
 */
@Configuration
public class OAuth2Config {

    @Bean
    public OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager() {
        DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(inMemoryClientRegistrationRepository(), authorizedClientRepository());

        OAuth2AuthorizedClientProvider auth2AuthorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
                .authorizationCode()
                .build();


        authorizedClientManager.setAuthorizedClientProvider(auth2AuthorizedClientProvider);
        return authorizedClientManager;
    }

    @Bean
    public  ClientRegistrationRepository inMemoryClientRegistrationRepository()  {



//java 翻墙 -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7890 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7890
        ClientRegistration clientRegistration = ClientRegistration.withRegistrationId("ali-pay")
                .clientId("270307511020-vv7av8q5ppuq965m923de2e9o7dm4qmd.apps.googleusercontent.com")
                .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
                .redirectUri("http://luojun99.xyz")
                .authorizationGrantType(AUTHORIZATION_CODE)
                .scope("profile")
                .tokenUri("https://openapi.alipay.com/gateway.do")
                .build();


        return new InMemoryClientRegistrationRepository(clientRegistration);
    }

    @Bean
    public OAuth2AuthorizedClientRepository authorizedClientRepository(){
        return new HttpSessionOAuth2AuthorizedClientRepository();
    }

}
